//
//  XTNewFeatureView.swift
//  MyBlog
//
//  Created by victor on 16/12/23.
//  Copyright © 2016年 victor. All rights reserved.
//

import UIKit
import SDWebImage

/// 新特性界面
class XTNewFeatureView: UIView {

    //MARK:属性
    fileprivate lazy var scrollView : UIScrollView = UIScrollView()
    
    fileprivate lazy var enterButton : UIButton = UIButton.cz_textButton(
        "进入微博",
        fontSize: 16,
        normalColor: UIColor.white,
        highlightedColor: UIColor.white,
        backgroundImageName: "new_feature_finish_button")
    
    fileprivate var pageControl : UIPageControl = UIPageControl()

    //MARK: 初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        
        pageControl.currentPage = 0
        pageControl.numberOfPages = 4
        pageControl.currentPageIndicatorTintColor = UIColor.orange
        pageControl.pageIndicatorTintColor = UIColor.black
        pageControl.isUserInteractionEnabled = false
        
        enterButton.addTarget(self, action: #selector(enterStatus), for: .touchUpInside)
        
        addSubview(scrollView)
        addSubview(enterButton)
        addSubview(pageControl)
        
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(self.snp.top)
            make.left.equalTo(self.snp.left)
            make.bottom.equalTo(self.snp.bottom)
            make.right.equalTo(self.snp.right)
        }
        
        enterButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.bottom.equalTo(self.snp.bottom).offset(-200)
        }
        
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.enterButton.snp.centerX)
            make.top.equalTo(self.enterButton.snp.bottom).offset(19)
        }
        
        enterButton.sizeToFit()
        
        setupImages()
    }
    
    fileprivate func setupImages() {
        let count = 4
        let rect = UIScreen.main.bounds
        
        for i in 0..<count {
            let imageName = "new_feature_\(i+1)"
            let imageView = UIImageView(image: UIImage(named: imageName))
            
            // 设置大小
            imageView.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
            scrollView.addSubview(imageView)
        }
        
        // 指定scrollView的属性
        scrollView.contentSize = CGSize(width: CGFloat(count + 1) * rect.width, height: rect.height)
        scrollView.bounces = false
        scrollView.isPagingEnabled = true
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        
        // 隐藏按钮
        enterButton.isHidden = true
    }
    
    // 进入主界面
    @objc fileprivate func enterStatus() {
        removeFromSuperview()
    }

}

extension XTNewFeatureView : UIScrollViewDelegate {
    
    // 停止拖拽
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        // 滚动到最后一屏，让视图删除
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        // 判断是否是最后一页
        if page == scrollView.subviews.count {
            removeFromSuperview()
        }
        
        // 如果是倒数第二页显示按钮
        enterButton.isHidden = (page != scrollView.subviews.count - 1)
    }
    
    // 拖拽中
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        // 一旦滚动隐藏按钮
        enterButton.isHidden = true
        
        // 计算当前偏移量
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        
        // 设置分页控件
        pageControl.currentPage = page
        
        // 分页控件的隐藏
        pageControl.isHidden = (page == scrollView.subviews.count)
    }
}
